package demo.jwt;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/jwt")
public class JwtController {
	
	private static final String USER = "admin";
	private static final String PWD = "123456";
	private static final String ERROR = "error";


	
	@RequestMapping(value="/login")
    public Map<String,Object> login(String user,String pwd){

        Map<String,Object> map = new HashMap<>();
        
        if( !USER.equals(user) || !PWD.equals(pwd)){
        	map.put(ERROR, "user or password error");
        	return map;
        }
        
        Map<String, String> usermap = new HashMap<>();
        usermap.put("user", user);
        usermap.put("pwd", pwd);
        
        String token = JwtUtil.genToken(usermap);
        
        map.put("token", token);
        map.put("user", user);
        map.put("data", "login success");

        return map;
    }
	
	
	@RequestMapping(value="/getUserInfo")
    public Map<String,Object> getUserInfo(String token){

        Map<String,Object> map = new HashMap<>();
        
        if(token==null){
        	map.put(ERROR, "token blank");
        	return map;
        }
        
        try{
       
        Map tokenmap = JwtUtil.parseToken(token);
        
        map.put("token", token);
        map.put("data", tokenmap);
        
        }catch(Throwable e){
        	map.put(ERROR, e.toString());
        	e.printStackTrace();

        }

        return map;
    }


}